<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *知识
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'项目列表'),
	array('op'=>'edit','title'=>'添加项目'),
);
$return['project_status']=$this->project_status;
$return['check_status']=$this->check_status;
$basic=$this->plugin_setting('basic');
$this->model('model')->check_gs($user);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$this->user_id);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['cname'])){
		$params['cname like']='%'.trim($_RQ['cname']).'%';
		$return['params']['cname']=$_RQ['cname'];
	}
	if(!empty($_RQ['sn'])){
		$params['sn like']='%'.trim($_RQ['sn']).'%';
		$return['params']['sn']=$_RQ['sn'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}else{
		if($_SESSION['os']!='windows'){
			$return['params']['status']=0;
			$params['status']=0;
		}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_baobiao_project',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('jy_baobiao_project_cuser',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['user_id']));
			$member=pdo_get('core_members',array('id'=>$item['mid']),array('credit1','credit2','mobile','avatar','nickname','username','sex'));
			$item['member']=$member;
			$item['user']=$user;
			$item['statustitle']=$this->project_status[$item['status']];
			$item['pidtitle']=pdo_getcolumn('jy_baobiao_project_group',array('id'=>$item['pid']),'title');
			$item['pidtitle']=$item['pidtitle']?:'无';
			$item['cidtitle']=pdo_getcolumn('jy_baobiao_project_group',array('id'=>$item['cid']),'title');
			$item['cidtitle']=$item['cidtitle']?:'无';
			$item['createtime']=date('Y-m-d',$item['createtime']);
			if($basic['nozhuanjp']){
				$item['nozhuanjp']=1;
			}else{
				$item['nozhuanjp']=0;
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'这里只显示待专家审核的项目，和刚审核的项目！','edit'=>false,'list'=>array(
			//array('name'=>'sn','title'=>'项目编码','type'=>'string'),
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('name'=>'user.cname','title'=>'申报单位','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'project_status'),
			//array('name'=>'fa_starttime','title'=>'发售起始时间','type'=>'time'),
			//array('name'=>'fa_edntime','title'=>'发售结束时间','type'=>'time'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'string'),
		),'action' =>array(
			 //array('type'=>'post','op'=>'daobaobiao','title'=>'导出申报表','show'=>array('status'=>array(1,8))),
			 array('type'=>'modal','op'=>'look10','title'=>'查看初审不通过原因','show'=>array('status'=>10)),
			 array('type'=>'modal','op'=>'look9','title'=>'查看协议复审不通过','show'=>array('status'=>9)),
			 array('type'=>'api','url'=>'/gongsi.project/uppdf01/','title'=>'预览申报表PDF'),
			 array('type'=>'api','url'=>'/gongsi.project/uppdf01/','title'=>'下载申报表PDF','show'=>array('status'=>array(1,2,3,8))),
			 //array('type'=>'href','op'=>'shbaobiao','title'=>'上传申报表','show'=>array('status'=>array(1,8))),
			 array('type'=>'href','op'=>'tianxieyi','title'=>'填报编辑协议书','show'=>array('status'=>array(3,4,9,11))),
			 //array('type'=>'post','op'=>'daoxieyi','title'=>'导出协议书','show'=>array('status'=>array(5,9))),
			 array('type'=>'api','url'=>'/gongsi.project/uppdf02/','title'=>'预览协议书PDF','show'=>array('status'=>array(3,4,5,6,7,8,9))),
			 array('type'=>'api','url'=>'/gongsi.project/uppdf02/','title'=>'下载协议书PDF','show'=>array('status'=>5)),
			 //array('type'=>'href','op'=>'shxieyi','title'=>'上传协议书','show'=>array('status'=>array(5,9))),
			 array('type'=>'href','op'=>'edit','title'=>'查看编辑','show'=>array('status'=>array(0,1,2,10))),
		)),
		'search'=>array(
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'project_status'),
		)
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_baobiao_project',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'),'star'=>3);
	}
	$cuser=pdo_get('jy_baobiao_project_cuser',array('id'=>$this->user_id,'uniacid'=>$_SESSION['uniacid']));
	if(!$cuser['cname'] || !$cuser['mobile'] || !$cuser['username']){
		exi('请先完善个人公司信息（联系人，联系电话，申报单位，资质文件等）','error','/gongsi.index/');
	}
	$cent_mb_data=array(
		array('data1'=>'产出指标','data2'=>'数量指标','nodel'=>1),
		array('data1'=>'产出指标','data2'=>'质量指标','nodel'=>1),
		array('data1'=>'效果指标','data2'=>'社会效益','nodel'=>1),
		array('data1'=>'效果指标','data2'=>'社会公众及服务对象满意度','nodel'=>1),
	);
	$item['zm_image']=$item['zm_image']?json_decode($item['zm_image'],true):array();
	$item['cent_workers']=$item['cent_workers']?json_decode($item['cent_workers'],true):array();
	$item['cent_mb_data']=$item['cent_mb_data']?json_decode($item['cent_mb_data'],true):$cent_mb_data;
	$basic=$this->plugin_setting('basic');
	$item['baobiaodemo']=tomedia($basic['baofile']);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('name'=>'baobiaodemo','title'=>'申报表模板','type'=>'copy','help'=>'复制地址下载'),
			//array('name'=>'baobiaofile','title'=>'上传申报表','type'=>'upfile'),
			array('name'=>'wname','title'=>'项目负责人','type'=>'string'),
			array('name'=>'wtel','title'=>'负责人电话','type'=>'string'),
			array('name'=>'wmobile','title'=>'负责人手机','type'=>'string'),
			array('name'=>'wcheng','title'=>'职务职称','type'=>'string'),
			array('name'=>'pid','title'=>'申报大项','type'=>'select2','options'=>'groups','title2'=>'子项','name2'=>'cid','options2'=>'cids'),
			array('name'=>'cent_tj','title'=>'申报条件','type'=>'text'),
			array('name'=>'cent_xm','title'=>'项目主要内容','type'=>'text'),
			array('name'=>'cent_mb','title'=>'项目目标及预期成效','type'=>'text'),
			array('type'=>'array','name'=>'cent_mb_data','title'=>'绩效指标','add_tip'=>'添加一个参指标','noadd'=>1,'nosplice'=>1,
								'list'=>array(
									array('type'=>'string','name'=>'data1','title'=>'一级指标'),	
									array('type'=>'string','name'=>'data2','title'=>'二级指标'),
									array('type'=>'string','name'=>'data3','title'=>'三级指标'),
									array('type'=>'string','name'=>'data4','title'=>'指标值'),
								),
								'details'=>array(
									array('type'=>'copy','name'=>'data1','title'=>'一级指标'),	
									array('type'=>'copy','name'=>'data2','title'=>'二级指标'),
									array('type'=>'text','name'=>'data3','title'=>'三级指标','help'=>'例子：开展**活动,活动验收合格率,**活动受益人数，学校老师学生满意度'),
									array('type'=>'string','name'=>'data4','title'=>'指标值','help'=>'例子：>**%，**次，**人'),
								)
			),
			array('name1'=>'cent_fee1','name2'=>'cent_fee2','title'=>'项目经费预算','title1'=>'总预算','unit1'=>'元','title2'=>'单位自筹','unit2'=>'元','type'=>'string_two'),
			array('name'=>'cent_fee3','title'=>'申请科协支持','type'=>'unitstring','unit'=>'元'),
			array('name'=>'cent_feedesc','title'=>'经费预算说明','type'=>'text'),
			array('name'=>'cent_ketitle','title'=>'课程名称','type'=>'string'),
			array('name'=>'cent_ketime','title'=>'课时数','type'=>'string'),
			array('name'=>'cent_jh','title'=>'项目实施步骤、进度计划','type'=>'text'),
			array('type'=>'array','name'=>'cent_workers','title'=>'项目负责人及主要参加人员','add_tip'=>'添加一个参加人',
								'list'=>array(
									array('type'=>'string','name'=>'name','title'=>'姓名'),	
									array('type'=>'string','name'=>'mobile','title'=>'联系方式'),
									array('type'=>'string','name'=>'zhiwu','title'=>'职务职称'),
									array('type'=>'string','name'=>'danwei','title'=>'工作单位'),
									array('type'=>'string','name'=>'desc','title'=>'项目中承担的主要工作'),
								),
								'details'=>array(
									array('type'=>'string','name'=>'name','title'=>'姓名'),	
									array('type'=>'string','name'=>'mobile','title'=>'联系方式'),
									array('type'=>'string','name'=>'zhiwu','title'=>'职务职称'),
									array('type'=>'string','name'=>'danwei','title'=>'工作单位'),
									array('type'=>'string','name'=>'desc','title'=>'项目中承担的主要工作'),
								)
			),
			array('name'=>'cent_jy','title'=>'项目申报单位意见','type'=>'text'),
			array('name'=>'anli','title'=>'备注','type'=>'text'),
			//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'project_status'),
		)
	);
	$return['groups']=pdo_getall('jy_baobiao_project_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>0),array('id','title'),'id');
	foreach($return['groups'] as &$gv){
		$gv['cids']=pdo_getall('jy_baobiao_project_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$gv['id']),array('id','title'),'id');
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['status'] && $postdata['status']!=10 && $postdata['status']!=2 && $postdata['status']!=0){
		exi('项目已进入审核流程，不能编辑！','error');
	}
	if(!$postdata['title']){
		exi('请填写项目名称！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'anli'=>trim($postdata['anli']),
		'zm_image'=>ijson_encode($postdata['zm_image']),
		'wname'=>trim($postdata['wname']),
		'wtel'=>trim($postdata['wtel']),
		'wmobile'=>trim($postdata['wmobile']),
		'wcheng'=>trim($postdata['wcheng']),
		'cent_tj'=>trim($postdata['cent_tj']),
		'cent_xm'=>trim($postdata['cent_xm']),
		'cent_mb'=>trim($postdata['cent_mb']),
		'cent_fee1'=>intval($postdata['cent_fee1']),
		'cent_fee2'=>intval($postdata['cent_fee2']),
		'cent_fee3'=>intval($postdata['cent_fee3']),
		'cent_jh'=>trim($postdata['cent_jh']),
		'cent_workers'=>ijson_encode($postdata['cent_workers']),
		'cent_jy'=>trim($postdata['cent_jy']),
		'type'=>intval($postdata['type']),
		'pid'=>intval($postdata['pid']),
		'cid'=>intval($postdata['cid']),
		'baobiaofile'=>trim($postdata['baobiaofile']),
		'status'=>0,
		'cent_mb_data'=>ijson_encode($postdata['cent_mb_data']),
		'cent_feedesc'=>trim($postdata['cent_feedesc']),
		'cent_ketitle'=>trim($postdata['cent_ketitle']),
		'cent_ketime'=>trim($postdata['cent_ketime']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_baobiao_project',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		//$selfcount=pdo_count('jy_baobiao_project',array('uniacid'=>$_SESSION['uniacid']),'id');
		//$sn='KPXM'.sprintf("%04d", $selfcount+1);
		$sn=get_ordersn($this->tablename('project'),'sn','',true);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['sn']=$sn;
		$data['user_id']=$this->user_id;
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_baobiao_project',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_baobiao_project',array('id'=>$id,'user_id'=>$this->user_id));
	$cuser=pdo_get('jy_baobiao_project_cuser',array('id'=>$this->user_id,'uniacid'=>$_SESSION['uniacid']));
	if(!$cuser['cname'] || !$cuser['mobile'] || !$cuser['cimage'] || !$cuser['username']){
		exi('请先完善个人公司信息（联系人，联系电话，公司名称，营业执照等）','error','/gongsi.index/');
	}
	$item['zm_image']=$item['zm_image']?json_decode($item['zm_image'],true):array();
	$item['pidtitle']=pdo_getcolumn('jy_baobiao_project_group',array('id'=>$item['pid']),'title');
	$item['cidtitle']=pdo_getcolumn('jy_baobiao_project_group',array('id'=>$item['cid']),'title');
	$item['statustitle']=$this->project_status[$item['status']];
	$return['item']=$item;
	$return['cuser']=$cuser;
	set_html('title','项目详情');
	exi($return);
}
if($_RQ['op']=='daobaobiao'){
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	$url=download_url('jy_baobiao/gongsi.project/down_baobiao/id='.$_RQ['id']);
	exi('','',$url);
}
if($_RQ['op']=='down_baobiao'){
	$this->model('project')->export_detail($_RQ['id']);
	exi($return);
}
if($_RQ['op']=='look10'){
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'初审不通过原因',
		'look'=>array(
			array('name'=>'remark','title'=>'原因','type'=>'text'),
		),
	);
	exi($return);
}
if($_RQ['op']=='look9'){
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'协议复审不通过',
		'look'=>array(
			array('name'=>'xy_remark','title'=>'原因','type'=>'text'),
		),
	);
	exi($return);
}
if($_RQ['op']=='uppdf01'){
	$id=$_RQ['id'];
	$to_file=$temp_dir."/{$item['title']}-项目申报PDF.pdf";
	$this->model('project')->export_detail($id,$to_file);
	exit;
}
if($_RQ['op']=='uppdf02'){
	$id=$_RQ['id'];
	$to_file=$temp_dir."/{$item['title']}-项目协议PDF.pdf";
	$this->model('project')->export_detail2($id,$to_file);
	exit;
}
if($_RQ['op']=='shbaobiao'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if($post['status']!=9 && $post['status']!=11 && $post['status']!=3 && $post['status']!=4){
			exi('当前状态不能编辑协议，不能编辑！','error');
		}
		$id=intval($post['id']);
		if(!$post['baobiaofile']){
			exi('请上传报表！','error');
		}
		pdo_update('jy_baobiao_project',array('baobiaofile'=>$post['baobiaofile'],'status'=>2),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('处理成功！','','list');
	}
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	$item['baobiaodemo']=SITEROOT.'/addons/jy_baobiao/resource/baobiaodemo.docx';
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'上传报表',
		'edit'=>array(
			array('name'=>'baobiaodemo','title'=>'申报表模板','type'=>'copy'),
			array('name'=>'baobiaofile','title'=>'上传报表','type'=>'upfile'),
		),
	);
	exi($return);
}
if($_RQ['op']=='tianxieyi'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$post['xy_xm']){
			exi('请填项目主要内容！','error');
		}
		pdo_update('jy_baobiao_project',array('xy_mb_data'=>ijson_encode($post['xy_mb_data']),'xy_xm'=>$post['xy_xm'],'xy_mb'=>$post['xy_mb'],'xy_jh'=>$post['xy_jh'],'xy_workers'=>ijson_encode($post['xy_workers']),'status'=>4),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('处理成功！','','list');
	}
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	$xy_mb_data=array(
		array('data1'=>'产出指标','data2'=>'数量指标','nodel'=>1),
		array('data1'=>'产出指标','data2'=>'质量指标','nodel'=>1),
		array('data1'=>'效果指标','data2'=>'社会效益','nodel'=>1),
		array('data1'=>'效果指标','data2'=>'社会公众及服务对象满意度','nodel'=>1),
	);
	$item['xy_mb_data']=$item['xy_mb_data']?json_decode($item['xy_mb_data'],true):$xy_mb_data;
	$item['xy_workers']=$item['xy_workers']?json_decode($item['xy_workers'],true):array();
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'填协议',
		'edit'=>array(
			array('name'=>'xy_xm','title'=>'项目内容','type'=>'text','help'=>''),
			array('name'=>'xy_jh','title'=>'项目实施步骤和进度计划','type'=>'text'),
			array('name'=>'xy_mb','title'=>'项目作用和预期效果','type'=>'text'),
			array('type'=>'array','name'=>'xy_workers','title'=>'项目负责人及主要参加人员','add_tip'=>'添加一个参加人',
								'list'=>array(
									array('type'=>'string','name'=>'name','title'=>'姓名'),	
									array('type'=>'string','name'=>'mobile','title'=>'联系方式'),
									array('type'=>'string','name'=>'zhiwu','title'=>'职务职称'),
									array('type'=>'string','name'=>'danwei','title'=>'工作单位'),
									array('type'=>'string','name'=>'desc','title'=>'项目中承担的主要工作'),
								),
								'details'=>array(
									array('type'=>'string','name'=>'name','title'=>'姓名'),	
									array('type'=>'string','name'=>'mobile','title'=>'联系方式'),
									array('type'=>'string','name'=>'zhiwu','title'=>'职务职称'),
									array('type'=>'string','name'=>'danwei','title'=>'工作单位'),
									array('type'=>'string','name'=>'desc','title'=>'项目中承担的主要工作'),
								)
			),
			array('type'=>'array','name'=>'xy_mb_data','title'=>'绩效指标','add_tip'=>'添加一个参指标','noadd'=>1,'nosplice'=>1,
								'list'=>array(
									array('type'=>'string','name'=>'data1','title'=>'一级指标'),	
									array('type'=>'string','name'=>'data2','title'=>'二级指标'),
									array('type'=>'string','name'=>'data3','title'=>'三级指标'),
									array('type'=>'string','name'=>'data4','title'=>'指标值'),
								),
								'details'=>array(
									array('type'=>'copy','name'=>'data1','title'=>'一级指标'),	
									array('type'=>'copy','name'=>'data2','title'=>'二级指标'),
									array('type'=>'text','name'=>'data3','title'=>'三级指标','help'=>'例子：开展**活动,产品验收合格率,**活动受益人数，社区群众满意度'),
									array('type'=>'string','name'=>'data4','title'=>'指标值','help'=>'例子：>**%，**次，**人'),
								)
			),
		),
	);
	exi($return);
}
if($_RQ['op']=='daoxieyi'){
	exi('开发中！','error');
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	exi($return);
}
if($_RQ['op']=='shxieyi'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$post['xieyifile']){
			exi('请上传协议！','error');
		}
		pdo_update('jy_baobiao_project',array('xieyifile'=>$post['xieyifile'],'status'=>6),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('处理成功！','','list');
	}
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'上传协议',
		'edit'=>array(
			array('name'=>'xieyifile','title'=>'上传协议','type'=>'upfile'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	exi('不能删除项目！','error');
}